package com.yanqu.road.dao.impl.activity.goldautumn;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnBuffInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnLatticeInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnLevelInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnScreedInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GoldAutumnConfigDaoImpl extends TempDao {

    private static String getGoldAutumnBuffInfoSql = "select * from `t_s_activity_mid_autumn_buff` where `activityId`=? ORDER BY `id` ASC";
    public List<GoldAutumnBuffInfo> getGoldAutumnBuffInfoList(int activityId) {
        List<GoldAutumnBuffInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoldAutumnBuffInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldAutumnBuffInfo data = new GoldAutumnBuffInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setParam(rs.getString("param"));
                data.setWeight(rs.getInt("weight"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoldAutumnLatticeInfoSql = "select * from `t_s_activity_mid_autumn_lattice` where `activityId`=? ORDER BY `id` ASC";
    public List<GoldAutumnLatticeInfo> getGoldAutumnLatticeInfoList(int activityId) {
        List<GoldAutumnLatticeInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoldAutumnLatticeInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldAutumnLatticeInfo data = new GoldAutumnLatticeInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setColorType(rs.getInt("colorType"));
                data.setType(rs.getInt("type"));
                data.setWeight(rs.getInt("weight"));
                //data.setParam(rs.getString("param"));
                data.setDesc(rs.getString("desc"));
                data.setSquareType(rs.getInt("squareType"));
                data.setStage(rs.getInt("stage"));
                String reward = rs.getString("reward");
                data.setReward(reward);
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoldAutumnLevelInfoSql = "select * from `t_s_activity_mid_autumn_level` where `activityId`=? ORDER BY `id` ASC";
    public List<GoldAutumnLevelInfo> getGoldAutumnLevelInfoList(int activityId) {
        List<GoldAutumnLevelInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoldAutumnLevelInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldAutumnLevelInfo data = new GoldAutumnLevelInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setUnlock(rs.getInt("unlock"));
                data.setCostNumber(rs.getInt("costNumber"));
                //data.setGetMagnification(rs.getInt("getMagnification"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGoldAutumnScreedInfoSql = "select * from `t_s_activity_mid_autumn_screed` where `activityId`=? ORDER BY `id` ASC";
    public List<GoldAutumnScreedInfo> getGoldAutumnScreedInfoList(int activityId) {
        List<GoldAutumnScreedInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGoldAutumnScreedInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldAutumnScreedInfo data = new GoldAutumnScreedInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setParam(rs.getString("param"));
                data.setWeight(rs.getInt("weight"));
                data.setUnlock(rs.getString("unlock"));
                List<Integer> rangeList = StringUtils.stringToIntegerList(data.getUnlock(), ";");
                if(rangeList.size() != 2){
                    throw new RuntimeException("月满金秋配置错误");
                }
                data.setMax(rangeList.get(1));
                data.setRangeList(rangeList);
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
